﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CNCSoft.EData;
using System.Data.SqlClient;

using Common;

namespace CNCSoft.ErpApps.TMS.Academic
{
    public partial class frmChuyenDiem : Form
    {
        DataTable dtS;
        DataTable dtD;

        public frmChuyenDiem()
        {
            InitializeComponent();
        }

        private void txtMaNienHocS_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNienHocS.Text == "") return;
                DataTable dt = new DataTable();
                SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_LOP WHERE MaNienHoc = '" + txtMaNienHocS.Text + "' Order by MaKhoi, STT");
                cboLopS.DataSource = dt;
                cboLopS.DisplayMember = "TenLop";
                cboLopS.ValueMember = "MaLop";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void txtMaNienHocD_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNienHocD.Text == "") return;
                DataTable dt = new DataTable();
                SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_LOP WHERE MaNienHoc = '" + txtMaNienHocD.Text + "' Order by MaKhoi, STT");

                cboLopD.DataSource = dt;
                cboLopD.DisplayMember = "TenLop";
                cboLopD.ValueMember = "MaLop";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void frmChuyenDiem_Load(object sender, EventArgs e)
        {
            txtMaNienHocS.Text = txtMaNienHocD.Text = GlobalVariable.WSInfo.getAppProperty("MaNienHocDefault", "");
        }

        private void cboHocSinh_S_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                dtS = new DataTable();
                SQLHelper.Fill(dtS, GlobalVariable.WSInfo.ConnectionString,  "SELECT * FROM HOCSINH_LOP_KQHT WHERE MaHocTap = '" + cboHocSinh_S.SelectedValue + "'");
                gridKQHT_S.DataSource = dtS;

                gridViewS.Columns["ID"].Visible = false;
                gridViewS.Columns["MaHocTap"].Visible = false;

                gridViewS.BestFitColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cboLopS_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT MaHocTap, HoLot + ' ' + Ten as HoTen FROM HOCSINH_LOP " + 
                                                                        "JOIN HOCSINH ON HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh " +
                                                                        "WHERE MaLop  = '" + cboLopS.SelectedValue + "' " +
                                                                        "AND MaTrangThai = 'CL' " +
                                                                        "Order by STT_SoCai");
                if (dt.Rows.Count == 0) cboHocSinh_S.SelectedIndex = -1 ;
                cboHocSinh_S.DataSource = dt;
                cboHocSinh_S.DisplayMember = "HoTen";
                cboHocSinh_S.ValueMember = "MaHocTap";

                cboHocSinh_S.Refresh();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void cboLopD_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (chkTatCa.Checked)
                    SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT MaHocTap, HoLot + ' ' + Ten as HoTen FROM HOCSINH_LOP " +
                                                                        "JOIN HOCSINH ON HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh " +
                                                                        "WHERE MaLop  = '" + cboLopD.SelectedValue + "' " +
                                                                        "AND MaTrangThai = 'SS' " +
                                                                        "Order by STT_SoCai");
                else //Loc nhung hoc hoc sinh co MaHocTap co chua chu "_copy"
                    SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT MaHocTap, HoLot + ' ' + Ten as HoTen FROM HOCSINH_LOP " +
                                                                        "JOIN HOCSINH ON HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh " +
                                                                        "WHERE MaLop  = '" + cboLopD.SelectedValue + "' " +
                                                                        "AND MaTrangThai = 'SS' " +
                                                                        "AND charindex('_Copy', MaHocTap) >0 " +
                                                                        "Order by STT_SoCai");
                if (dt.Rows.Count == 0) cboHocSinh_D.SelectedIndex = -1;
                cboHocSinh_D.DataSource = dt;
                cboHocSinh_D.DisplayMember = "HoTen";
                cboHocSinh_D.ValueMember = "MaHocTap";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void cboHocSinh_D_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                dtD = new DataTable();
                SQLHelper.Fill(dtD, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM HOCSINH_LOP_KQHT WHERE MaHocTap = '" + cboHocSinh_D.SelectedValue + "'");
                gridKQHT_D.DataSource = dtD;

                gridViewD.Columns["ID"].Visible = false;
                gridViewD.Columns["MaHocTap"].Visible = false;

                gridViewD.BestFitColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdFromSToD_Click(object sender, EventArgs e)
        {
            if (cboHocSinh_D.SelectedIndex == -1 || cboHocSinh_S.SelectedIndex == -1)
                return;

            //Canh bao truoc
            if (MessageBox.Show("Lưu ý: TMS sẽ xóa tất cả các điểm của học sinh " + cboHocSinh_D.Text + ", lớp " + cboLopD.Text + " và chuyển điểm mới\n\nBạn có chắc chắn không ???", "Cẩn thận", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                 == System.Windows.Forms.DialogResult.No)
                return;


            try
            {
                //Delete KQHT cu, Insert KQHT moi
                string strStore = "sp_GiaoVu_ChuyenDiemChoMotHocSinh @pMaHocTap_S = '" + cboHocSinh_S.SelectedValue + "', @pMaHocTap_D = '" + cboHocSinh_D.SelectedValue + "'";
                SQLHelper.ExecuteNonQuery(GlobalVariable.WSInfo.ConnectionString, strStore);

                //Fill lai
                cboHocSinh_D_SelectedIndexChanged(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
